/*
 * MyGWT User Extensions
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.mygwt.ux.client.widgets;

import net.mygwt.ui.client.MyDOM;
import net.mygwt.ui.client.MyGWT;
import net.mygwt.ui.client.util.Format;
import net.mygwt.ui.client.widget.Dialog;
import net.mygwt.ui.client.widget.WidgetContainer;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;

/**
 * 
 * @author ggeorg
 * 
 */
public class InputBox extends Dialog {

  private String message, value;
  private Element input;

  /**
   * Creates a new message box instance with the given icon and button(s).
   * 
   * @param icon the icon to display
   * @param style the buttons to display
   */
  public InputBox(int style) {
    super(style);
    setSize(320, 125);
    setCloseOnButtonClick(true);
  }

  /**
   * Returns the dialog's message.
   * 
   * @return the message
   */
  public String getMessage() {
    return message;
  }

  /**
   * Sets the dialog's message.
   * 
   * @param message the message
   */
  public void setMessage(String message) {
    this.message = message;
  }

  protected void createContent(WidgetContainer container) {
    StringBuffer sb = new StringBuffer();
    sb.append("<table width=100% height=100%><tbody><tr>");
    sb.append("<td class='my-mbox-icon'><div class='my-mbox-icon my-mbox-question'></div></td>");
    sb.append("<td width=100% class=my-mbox-text><span>{0}</span><input  style='width: 100%;' value='{1}'></td>");
    sb.append("</tr></tbody></table>");

    String html = Format.substitute(sb.toString(), new String[] {message, value});
    DOM.setInnerHTML(container.getElement(), html);

    input = DOM.getChild(DOM.getChild(MyDOM.getSubChild(container.getElement(), 3), 1),1);

    if (!MyGWT.isIE) {
      DOM.setElementPropertyInt(input, "tabIndex", 0);
    }

    MyDOM.setFocus(input, true);
  }

  protected void onRender() {
    super.onRender();
    addStyleName("my-message-box");
    addStyleName("my-shell-plain");
  }

  public void setValue(final String value) {
    this.value = value;
    if (rendered && input != null) {
      DOM.setElementProperty(input, "value", value);
    }
  }

  public String getValue() {
    return DOM.getElementProperty(input, "value");
  }
}
